using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using gudusoft.gsqlparser;
using gudusoft.gsqlparser.Units;


namespace jointabledetail
{
    class jointabledetail
    {
        static void Main(string[] args)
        {
            string lcsqltext = "";
            //Reading from file
            try
            {
                StreamReader re = File.OpenText("t.sql");
                string input = null;
                while ((input = re.ReadLine()) != null)
                {
                    lcsqltext = lcsqltext + Environment.NewLine + input;
                }
            }
            catch (Exception e)
            {
                // Let the user know what went wrong.
                Console.WriteLine("The file doesn't exist:");
                Console.WriteLine(e.Message);
            }


            Console.WriteLine("Starting ...");

            TGSqlParser parser = new TGSqlParser(TDbVendor.DbVMssql);

            parser.SqlText.Text = lcsqltext;
            Console.WriteLine("Parsing...");
            int i = parser.Parse();
            if (i == 0)
            {
                Console.WriteLine("Parse sucessfully");
                TSelectSqlStatement asql;
                asql = (TSelectSqlStatement)parser.SqlStatements[0];
                IterateJoinlist(asql.JoinTables);
            }
            else
            {
                Console.WriteLine(parser.ErrorMessages);
            }

        }//main
        public static void IterateJoinlist(TLzJoinList pcJoins)
        {
            for (int i = 0; i < pcJoins.Count(); i++)
            {
                TLzJoin lcJoin = pcJoins[i];
                VisitJoin(lcJoin);
            }
        }

        public static void VisitJoin(TLzJoin pcJoin)
        {
            Console.WriteLine("Join Type:{0}", pcJoin.JoinTableType);
            Console.WriteLine("Nested level:{0}", pcJoin.NestedLevel);
            Console.WriteLine("Join alias:{0}", pcJoin.Alias);

            switch (pcJoin.JoinTableType)
            {
                case (TJoinTableType.jttNone):
                    {
                        Console.WriteLine("Table:{0}", pcJoin.JoinTable.AsText);
                        break;
                    };
                case (TJoinTableType.jttTable):
                    {
                        Console.WriteLine("Table:{0}", pcJoin.JoinTable.AsText);
                        break;
                    };
                case (TJoinTableType.jttJoin):
                    {
                        Console.WriteLine("Join");
                        VisitJoin(pcJoin.JoinJoin);
                        break;
                    };
            }
            IterateJoinItems(pcJoin.JoinItems);
        }

        public static void IterateJoinItems(TLzJoinItemList pcJoinItems)
        {
            for (int i = 0; i < pcJoinItems.Count(); i++)
            {
                VisitJoinItem(pcJoinItems[i]);
            }
        }

        public static void VisitJoinItem(TLzJoinItem pcJoinItem)
        {
            if (pcJoinItem.JoinItemTableType == TJoinTableType.jttTable)
            {
                Console.WriteLine("Table:{0}", pcJoinItem.JoinItemTable.AsText);
            }
            else
                VisitJoin(pcJoinItem.JoinItemJoin);
        }
    }
}
